﻿using System;

namespace System
{
    class Program
    {
        static void Main(string[] args)
        {
            //int[] arr = { 9, 8, 7, 8, 7, 6, 7, 6, 5, 6, 5, 4 };
            int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Console.WriteLine("before:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine("");
            DateTime StartTime = DateTime.Now;
            BubbleSort(arr,arr.Length);
            DateTime EndTime = DateTime.Now;
            TimeSpan ts = EndTime - StartTime;
            Console.WriteLine("after:");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine("");
            Console.WriteLine("Time Spend:" + ts);
            Console.ReadLine();
        }
        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="arr">数组</param>
        /// <param name="length">数组长度</param>
        public static void BubbleSort(int[] arr, int length)
        {
            //是否有交换记录
            bool change = true;
            for (var i = 0; i < length - 1 && change; i++)
            {
                change = false;
                for (var j = 0; j < length - 1; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        SortHelper.Swap(arr, j, j + 1);
                        change = true;
                    }
                }
            }
        }
    }
}